<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LANGUAGE RankNTypes #-}</span><span>
</span><span id="line-2"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-3"></span><span class="hs-comment">-- |</span><span>
</span><span id="line-4"></span><span class="hs-comment">-- Module      :  Data.Array.ST</span><span>
</span><span id="line-5"></span><span class="hs-comment">-- Copyright   :  (c) The University of Glasgow 2001</span><span>
</span><span id="line-6"></span><span class="hs-comment">-- License     :  BSD-style (see the file libraries/base/LICENSE)</span><span>
</span><span id="line-7"></span><span class="hs-comment">--</span><span>
</span><span id="line-8"></span><span class="hs-comment">-- Maintainer  :  libraries@haskell.org</span><span>
</span><span id="line-9"></span><span class="hs-comment">-- Stability   :  experimental</span><span>
</span><span id="line-10"></span><span class="hs-comment">-- Portability :  non-portable (uses Data.Array.MArray)</span><span>
</span><span id="line-11"></span><span class="hs-comment">--</span><span>
</span><span id="line-12"></span><span class="hs-comment">-- Mutable boxed and unboxed arrays in the 'Control.Monad.ST.ST' monad.</span><span>
</span><span id="line-13"></span><span class="hs-comment">--</span><span>
</span><span id="line-14"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-15"></span><span>
</span><span id="line-16"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">Data.Array.ST</span><span> </span><span class="hs-special">(</span><span>
</span><span id="line-17"></span><span>   </span><span class="annot"><span class="hs-comment">-- * Boxed arrays</span></span><span>
</span><span id="line-18"></span><span>   </span><span class="annot"><a href="../../base/src/GHC.Arr.html#STArray"><span class="hs-identifier">STArray</span></a></span><span class="hs-special">,</span><span>             </span><span class="hs-comment">-- instance of: Eq, MArray</span><span>
</span><span id="line-19"></span><span>   </span><span class="annot"><a href="Data.Array.ST.html#runSTArray"><span class="hs-identifier">runSTArray</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-20"></span><span>
</span><span id="line-21"></span><span>   </span><span class="annot"><span class="hs-comment">-- * Unboxed arrays</span></span><span>
</span><span id="line-22"></span><span>   </span><span class="annot"><a href="Data.Array.Base.html#STUArray"><span class="hs-identifier">STUArray</span></a></span><span class="hs-special">,</span><span>            </span><span class="hs-comment">-- instance of: Eq, MArray</span><span>
</span><span id="line-23"></span><span>   </span><span class="annot"><a href="Data.Array.ST.html#runSTUArray"><span class="hs-identifier">runSTUArray</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-24"></span><span>
</span><span id="line-25"></span><span>   </span><span class="annot"><span class="hs-comment">-- * Overloaded mutable array interface</span></span><span>
</span><span id="line-26"></span><span>   </span><span class="hs-keyword">module</span><span> </span><span class="annot"><a href="Data.Array.MArray.html"><span class="hs-identifier">Data.Array.MArray</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-27"></span><span> </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-28"></span><span>
</span><span id="line-29"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Data.Array.Base.html"><span class="hs-identifier">Data.Array.Base</span></a></span><span>  </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="Data.Array.Base.html#STUArray"><span class="hs-identifier">STUArray</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Array.Base.html#UArray"><span class="hs-identifier">UArray</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Array.Base.html#unsafeFreezeSTUArray"><span class="hs-identifier">unsafeFreezeSTUArray</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-30"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Data.Array.MArray.html"><span class="hs-identifier">Data.Array.MArray</span></a></span><span>
</span><span id="line-31"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/Control.Monad.ST.html#"><span class="hs-identifier">Control.Monad.ST</span></a></span><span> </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="../../base/src/GHC.ST.html#ST"><span class="hs-identifier">ST</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../base/src/GHC.ST.html#runST"><span class="hs-identifier">runST</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-32"></span><span>
</span><span id="line-33"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/GHC.Arr.html#"><span class="hs-identifier">GHC.Arr</span></a></span><span>          </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="../../base/src/GHC.Arr.html#STArray"><span class="hs-identifier">STArray</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../base/src/GHC.Arr.html#Array"><span class="hs-identifier">Array</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../base/src/GHC.Arr.html#unsafeFreezeSTArray"><span class="hs-identifier">unsafeFreezeSTArray</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-34"></span><span>
</span><span id="line-35"></span><span class="hs-comment">-- | A safe way to create and work with a mutable array before returning an</span><span>
</span><span id="line-36"></span><span class="hs-comment">-- immutable array for later perusal.  This function avoids copying</span><span>
</span><span id="line-37"></span><span class="hs-comment">-- the array before returning it - it uses 'unsafeFreeze' internally, but</span><span>
</span><span id="line-38"></span><span class="hs-comment">-- this wrapper is a safe interface to that function.</span><span>
</span><span id="line-39"></span><span class="hs-comment">--</span><span>
</span><span id="line-40"></span><span id="local-6989586621679054444"><span id="local-6989586621679054445"><span class="annot"><a href="Data.Array.ST.html#runSTArray"><span class="hs-identifier hs-type">runSTArray</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">(</span><span class="hs-keyword">forall</span><span> </span><span id="local-6989586621679054446"><span class="annot"><a href="#local-6989586621679054446"><span class="hs-identifier hs-type">s</span></a></span></span><span> </span><span class="hs-operator">.</span><span> </span><span class="annot"><a href="../../base/src/GHC.ST.html#ST"><span class="hs-identifier hs-type">ST</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679054446"><span class="hs-identifier hs-type">s</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../base/src/GHC.Arr.html#STArray"><span class="hs-identifier hs-type">STArray</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679054446"><span class="hs-identifier hs-type">s</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679054445"><span class="hs-identifier hs-type">i</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679054444"><span class="hs-identifier hs-type">e</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../base/src/GHC.Arr.html#Array"><span class="hs-identifier hs-type">Array</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679054445"><span class="hs-identifier hs-type">i</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679054444"><span class="hs-identifier hs-type">e</span></a></span></span></span><span>
</span><span id="line-41"></span><span id="runSTArray"><span class="annot"><span class="annottext">runSTArray :: forall i e. (forall s. ST s (STArray s i e)) -&gt; Array i e
</span><a href="Data.Array.ST.html#runSTArray"><span class="hs-identifier hs-var hs-var">runSTArray</span></a></span></span><span> </span><span id="local-6989586621679054412"><span class="annot"><span class="annottext">forall s. ST s (STArray s i e)
</span><a href="#local-6989586621679054412"><span class="hs-identifier hs-var">st</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(forall s. ST s (Array i e)) -&gt; Array i e
forall a. (forall s. ST s a) -&gt; a
</span><a href="../../base/src/GHC.ST.html#runST"><span class="hs-identifier hs-var">runST</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ST s (STArray s i e)
forall s. ST s (STArray s i e)
</span><a href="#local-6989586621679054412"><span class="hs-identifier hs-var">st</span></a></span><span> </span><span class="annot"><span class="annottext">ST s (STArray s i e)
-&gt; (STArray s i e -&gt; ST s (Array i e)) -&gt; ST s (Array i e)
forall (m :: * -&gt; *) a b. Monad m =&gt; m a -&gt; (a -&gt; m b) -&gt; m b
</span><a href="../../base/src/GHC.Base.html#%3E%3E%3D"><span class="hs-operator hs-var">&gt;&gt;=</span></a></span><span> </span><span class="annot"><span class="annottext">STArray s i e -&gt; ST s (Array i e)
forall s i e. STArray s i e -&gt; ST s (Array i e)
</span><a href="../../base/src/GHC.Arr.html#unsafeFreezeSTArray"><span class="hs-identifier hs-var">unsafeFreezeSTArray</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-42"></span><span>
</span><span id="line-43"></span><span class="hs-comment">-- | A safe way to create and work with an unboxed mutable array before</span><span>
</span><span id="line-44"></span><span class="hs-comment">-- returning an immutable array for later perusal.  This function</span><span>
</span><span id="line-45"></span><span class="hs-comment">-- avoids copying the array before returning it - it uses</span><span>
</span><span id="line-46"></span><span class="hs-comment">-- 'unsafeFreeze' internally, but this wrapper is a safe interface to</span><span>
</span><span id="line-47"></span><span class="hs-comment">-- that function.</span><span>
</span><span id="line-48"></span><span class="hs-comment">--</span><span>
</span><span id="line-49"></span><span id="local-6989586621679054426"><span id="local-6989586621679054427"><span class="annot"><a href="Data.Array.ST.html#runSTUArray"><span class="hs-identifier hs-type">runSTUArray</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">(</span><span class="hs-keyword">forall</span><span> </span><span id="local-6989586621679054428"><span class="annot"><a href="#local-6989586621679054428"><span class="hs-identifier hs-type">s</span></a></span></span><span> </span><span class="hs-operator">.</span><span> </span><span class="annot"><a href="../../base/src/GHC.ST.html#ST"><span class="hs-identifier hs-type">ST</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679054428"><span class="hs-identifier hs-type">s</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="Data.Array.Base.html#STUArray"><span class="hs-identifier hs-type">STUArray</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679054428"><span class="hs-identifier hs-type">s</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679054427"><span class="hs-identifier hs-type">i</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679054426"><span class="hs-identifier hs-type">e</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="Data.Array.Base.html#UArray"><span class="hs-identifier hs-type">UArray</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679054427"><span class="hs-identifier hs-type">i</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679054426"><span class="hs-identifier hs-type">e</span></a></span></span></span><span>
</span><span id="line-50"></span><span id="runSTUArray"><span class="annot"><span class="annottext">runSTUArray :: forall i e. (forall s. ST s (STUArray s i e)) -&gt; UArray i e
</span><a href="Data.Array.ST.html#runSTUArray"><span class="hs-identifier hs-var hs-var">runSTUArray</span></a></span></span><span> </span><span id="local-6989586621679054409"><span class="annot"><span class="annottext">forall s. ST s (STUArray s i e)
</span><a href="#local-6989586621679054409"><span class="hs-identifier hs-var">st</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(forall s. ST s (UArray i e)) -&gt; UArray i e
forall a. (forall s. ST s a) -&gt; a
</span><a href="../../base/src/GHC.ST.html#runST"><span class="hs-identifier hs-var">runST</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ST s (STUArray s i e)
forall s. ST s (STUArray s i e)
</span><a href="#local-6989586621679054409"><span class="hs-identifier hs-var">st</span></a></span><span> </span><span class="annot"><span class="annottext">ST s (STUArray s i e)
-&gt; (STUArray s i e -&gt; ST s (UArray i e)) -&gt; ST s (UArray i e)
forall (m :: * -&gt; *) a b. Monad m =&gt; m a -&gt; (a -&gt; m b) -&gt; m b
</span><a href="../../base/src/GHC.Base.html#%3E%3E%3D"><span class="hs-operator hs-var">&gt;&gt;=</span></a></span><span> </span><span class="annot"><span class="annottext">STUArray s i e -&gt; ST s (UArray i e)
forall s i e. STUArray s i e -&gt; ST s (UArray i e)
</span><a href="Data.Array.Base.html#unsafeFreezeSTUArray"><span class="hs-identifier hs-var">unsafeFreezeSTUArray</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-51"></span><span>
</span><span id="line-52"></span><span>
</span><span id="line-53"></span><span class="hs-comment">-- INTERESTING... this is the type we'd like to give to runSTUArray:</span><span>
</span><span id="line-54"></span><span class="hs-comment">--</span><span>
</span><span id="line-55"></span><span class="hs-comment">-- runSTUArray :: (Ix i, IArray UArray e,</span><span>
</span><span id="line-56"></span><span class="hs-comment">--              forall s. MArray (STUArray s) e (ST s))</span><span>
</span><span id="line-57"></span><span class="hs-comment">--         =&gt; (forall s . ST s (STUArray s i e))</span><span>
</span><span id="line-58"></span><span class="hs-comment">--         -&gt; UArray i e</span><span>
</span><span id="line-59"></span><span class="hs-comment">--</span><span>
</span><span id="line-60"></span><span class="hs-comment">-- Note the quantified constraint.  We dodged the problem by using</span><span>
</span><span id="line-61"></span><span class="hs-comment">-- unsafeFreezeSTUArray directly in the defn of runSTUArray above, but</span><span>
</span><span id="line-62"></span><span class="hs-comment">-- this essentially constrains us to a single unsafeFreeze for all STUArrays</span><span>
</span><span id="line-63"></span><span class="hs-comment">-- (in theory we might have a different one for certain element types).</span><span>
</span><span id="line-64"></span></pre></body></html>